package com.foodplus.www.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.api.RetrofitManager;
import com.foodplus.www.api.services.BasketService;
import com.foodplus.www.bean.basket.AddGoodBean;
import com.foodplus.www.bean.good.siplegood.GoodsInfoBean;
import com.foodplus.www.bean.good.siplegood.SipleGoodBean;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.BasketPresenter;
import com.foodplus.www.presenter.HomePresenter;
import com.foodplus.www.presenter.SipleGoodPresenter;
import com.foodplus.www.view.impl.BasketView;
import com.foodplus.www.view.impl.SipleGoodView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SipGoodActivity extends AppCompatActivity implements SipleGoodView {

    //UI
    @BindView(R.id.sipshop_toptitle)
    TextView sipgoodTitleTv;
    @BindView(R.id.sipshop_banner)
    Banner sipshopBanner;
    @BindView(R.id.sipshop_title)
    TextView sipshopTitle;
    @BindView(R.id.sipshop_price)
    TextView sipshopPrice;
    @BindView(R.id.sipshop_oldprice)
    TextView sipshopOldprice;
    @BindView(R.id.sipshop_person)
    TextView sipshopPerson;
    @BindView(R.id.sipshop_pro)
    TextView sipshopPro;
    @BindView(R.id.sipshop_more)
    TextView sipshopMore;
    @BindView(R.id.sipshop_btmore)
    ImageView sipshopBtmore;
    @BindView(R.id.sipshop_commit)
    TextView sipshopCommit;
    @BindView(R.id.sipshop_btcommit)
    ImageView sipshopBtcommit;
    @BindView(R.id.sipgood_back)
    ImageView ivBack;
    @BindView(R.id.activity_sip_good)
    RelativeLayout layout;
    @BindView(R.id.shop_joinBasket)
    TextView shopJoinBasket;
//    @BindView(R.id.sipshop_toptitle)
//    TextView sipshopToptitle;

    BasketView view;

    //产品的ID
    private String id;

    //p层数据源
    private SipleGoodPresenter presenter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sip_good);
        ButterKnife.bind(this);
        //获取产品的ID
        Intent intent = getIntent();
        id = intent.getStringExtra("id");

        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getBack();
            }
        });

        //在没载入数据前隐藏布局
        setOffVision();
        //获取数据
        getData();
    }

    private void getData() {
        presenter = new SipleGoodPresenter(this);
        presenter.getSipGoodsData(ParamManager.getInstance().getSipGoodParams(id));
    }

    @Override
    public void onSuccess(SipleGoodBean sigleGoodBean) {
        //整理数据
        GoodsInfoBean bean = sigleGoodBean.getDatas().getGoods_info();

        List<String> bannerData = new ArrayList<>();
        String[] bannerStrs = sigleGoodBean.getDatas().getGoods_image().split(",");
        for (int i = 0; i < bannerStrs.length; i++) {
            bannerData.add(bannerStrs[i]);
        }
        //载入数据
        sipshopBanner.setImages(bannerData);
        sipshopBanner.setImageLoader(new MyImageLoad());
        sipshopBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        sipshopBanner.setIndicatorGravity(BannerConfig.CENTER);
        sipshopBanner.isAutoPlay(false);
        sipshopBanner.start();

        sipshopTitle.setText(bean.getGoods_name());
        sipshopPro.setText(bean.getGoods_jingle());
        sipshopPrice.setText("¥" + bean.getGoods_promotion_price());
        sipshopOldprice.setText("原价：¥" + bean.getGoods_marketprice());
        sipshopPerson.setText("已售：" + bean.getGoods_salenum() + "件");

        //布局显示
        setOnVision();

    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {
        System.out.println();
    }

    @Override
    public void onFinish() {

    }

    private void getBack() {
        SipGoodActivity.this.overridePendingTransition(0, R.anim.shop_trans_out);
        SipGoodActivity.this.finish();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        getBack();
    }

    private void setOnVision() {
        layout.setVisibility(View.VISIBLE);
    }

    private void setOffVision() {
        layout.setVisibility(View.INVISIBLE);
    }

    /**
     * 判断服务器传过来的数据是否是添加购物车成功
     *
     * @param addGoodBean
     */
    @Override
    public void onReceiveAddData(AddGoodBean addGoodBean) {
        if (addGoodBean != null && addGoodBean.getCode() == 200 && addGoodBean.getDatas().getSuccess().equals("添加成功"))
        {
            Toast.makeText(this, "添加成功", Toast.LENGTH_LONG).show();
        }else {
            Toast.makeText(this, "添加失败", Toast.LENGTH_LONG).show();
        }
    }

    public class MyImageLoad implements ImageLoader {
        @Override
        public void displayImage(Context context, Object path, ImageView imageView) {
            Glide.with(context).load(path).placeholder(R.mipmap.bg_morentu).fitCenter().into(imageView);
        }
    }

    @OnClick(R.id.shop_joinBasket)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.shop_joinBasket:
                presenter.addToBasket(ParamManager.getInstance().addGoodMap(id));
                break;
        }
    }

}
